package com.cenjiawen.common;

import com.bk.routes.BKRoutes;
import com.jfinal.config.Constants;
import com.jfinal.config.Interceptors;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.zhenyee.explore.controller.ExploreController;
import com.zhenyi.api.ApiController;
import com.zhenyi.boya.core.BYRoutes;
import com.zhenyi.config.XHCRoutes;
import com.zhenyi.dingke.config.DKRoutes;
import com.zhenyi.etcr.index.admin.SeoController;
import com.zhenyi.jiahua.core.JHRoutes;
import com.zhenyi.liuxue.core.LXRoutes;
import com.zhenyi.sms.SMSController;
import com.zhenyi.toupiao.core.TPRoutes;
import com.zhenyi.websocket.core.ws.WSServer;
import com.zhenyi.websocket.core.ws.WSUtilsPro;
import com.zhenyi.websocket.core.ws.WebSocketChannelInitializer2;
import com.zhenyi.wumian.config.WMRoutes;
import com.zhenyi.wumian.ws.Handler;
import com.zhenyi.youqu.config.YQRoutes;
import com.zhenyi.zhaozheng.config.ZZRoutes;
import com.zhenyi.zhengfang.config.ZFRoutes;

import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.RandomUtil;
import freemarker.template.Configuration;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class BeikangAyiConfig extends Config {

	public static void main(String[] args) {
		// Hashids hashids = new Hashids("pingguotuisms", 8);
		// Hashids question = new Hashids("question", 8);
		// for (int i = 10000; i < 10005; i++) {
		// System.out.println("question:" + question.encode(i));
		// }
		// System.out.println();
		// String password = hashids.encode(1L);
		// for (int i = 10000; i < 10010; i++) {
		// String account = hashids.encode(i);
		// System.out.println("account:\n" + account + "@yandex.com");
		// }
		// System.out.println("password:" + password);

		String key = RandomUtil.randomString(16);
		System.out.println(key);
		JFinal.start("ROOT", 9090, "/");
		// D7DpRG75@yandex.com
		// D7DpRG75@protonmail.com
		// appleid password 6GyWJ5XZ
		// mail password nD7Dr45e
	}

	@Override
	public void configConstant(Constants me) {
		super.configConstant(me);
		Configuration cfg = FreeMarkerRender.getConfiguration();
		cfg.setDefaultEncoding("utf-8");
		// cfg.setSharedVariable("shiro", new ShiroTags());
	}

	@Override
	public void configRoute(Routes me) {
		// if (PropKit.getBoolean("EN_BEIKANG", false)) {
		// me.add(new AYRoutes());
		// }
		// me.add(new LMRoutes());
		// me.add(new DTRoutes());
		if (PropKit.getBoolean("EN_TOUPIAO", false)) {
			me.add(new TPRoutes());
		}
		if (PropKit.getBoolean("EN_WUMAIN", false)) {
			me.add(new WMRoutes());
		}
		if (PropKit.getBoolean("EN_BOYA", false)) {
			me.add(new BYRoutes());
		}
		if (PropKit.getBoolean("EN_LIUXUE", false)) {
			me.add(new LXRoutes());
		}
		if (PropKit.getBoolean("EN_JIAHUA", false)) {
			me.add(new JHRoutes());
		}
		if (PropKit.getBoolean("EN_XHC", false)) {
			me.add(new XHCRoutes());
		}
		if (PropKit.getBoolean("EN_YOUQU", false)) {
			me.add(new YQRoutes());
		}
		if (PropKit.getBoolean("EN_DingKe", false)) {
			me.add(new DKRoutes());
		}
		if (PropKit.getBoolean("EN_Booking", false)) {
			me.add(new BKRoutes());
		}
		if (PropKit.getBoolean("EN_SMS", false)) {
			me.add("/sms", SMSController.class);
		}
		if (PropKit.getBoolean("EN_ZHAOZHENG", false)) {
			me.add(new ZZRoutes());
		}
		if (PropKit.getBoolean("EN_ZHENGFANG", false)) {
			me.add(new ZFRoutes());
		}
		if (PropKit.getBoolean("EN_YITAI_CMS", false)) {
			me.add("/yitai", SeoController.class);
		}

		// me.add("api/fenci", FenciController.class);
		me.add("api", ApiController.class);
		// me.add(new PXLMRoutes());
		me.add("/explore", ExploreController.class);
		// me.add("/sms", SMSController.class);
	}

	@Override
	public void configEngine(Engine me) {
	}

	@Override
	protected void configSQL(ActiveRecordPlugin arp) {
		super.configSQL(arp);
		// arp.addSqlTemplate("limian.sql");
	}

	@Override
	protected void configSQLConnection(Plugins me) {
		boolean debug = PropKit.getBoolean("devMode", false);
		if (!debug) {
		}
		// addDataSource("dev.zhen-yee.com", "yitai", "root", "Cendb", me, new
		// ArpHandler() {
		//
		// @Override
		// public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
		// com.limian.common.model._MappingKit.mapping(arp);
		// arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		// arp.addSqlTemplate("limian.sql");
		// return arp;
		// }
		// });
		if (!debug) {

		}

		if (PropKit.getBoolean("EN_BEIKANG", false)) {
			super.configSQLConnection(me);
		}

		if (PropKit.getBoolean("EN_PXLM", false)) {
			String host = PropKit.get("PXLM_DB", "dev.zhen-yee.com");
			String password = PropKit.get("PXLM_DB_PWD", "Cendb");
			addDataSource(host, "pxlm", "root", password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.pxlm.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("pxlmProduct.txt");
					return arp;
				}
			});
		}

		if (PropKit.getBoolean("EN_Booking", false)) {
			String host = PropKit.get("BK_DB", "dev.zhen-yee.com");
			String password = PropKit.get("BK_DB_PWD", "Cendb");
			addDataSource(host, "booking", "root", password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.moomi.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("booking/booking.txt");
					return arp;
				}
			});
		}

		if (PropKit.getBoolean("EN_TOUPIAO", false)) {
			// addDataSource("test.zhen-yee.com", "toupiao", "root", "zhenyi",
			// me, new ArpHandler() {
			addDataSource("192.168.50.222", "toupiao", "root", "zhenyi", me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.toupiao.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("toupiao.sql");
					return arp;
				}
			});
		}

		if (PropKit.getBoolean("EN_LIUXUE", false)) {
			// addDataSource("zhenyi.asuscomm.com:9306", "liuxue", "root",
			// "zhenyi", me, new ArpHandler() {
			String host = PropKit.get("LX_DB", "192.168.50.222");
			String password = PropKit.get("LX_DB_PWD", "zhenyi");
			String user = PropKit.get("LX_DB_USER", "root");
			addDataSource(host, "liuxue", user, password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.liuxue.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("liuxue/liuxue.txt");
					arp.addSqlTemplate("liuxue/liuxueImport.txt");
					return arp;
				}
			});
		}
		if (PropKit.getBoolean("EN_XHC", false)) {
			String host = PropKit.get("XHC_DB", "192.168.50.222");
			String password = PropKit.get("XHC_DB_PWD", "zhenyi");
			addDataSource(host, "xhc", "root", password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.xhc.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("xhc.sql");
					arp.addSqlTemplate("xhcAdmin.sql");
					return arp;
				}
			});

		}

		if (PropKit.getBoolean("EN_BOYA", false)) {
			addDataSource(PropKit.get("BOYA_DB", "192.168.50.222"), "boya", "root",
					PropKit.get("BOYA_DB_PWD", "zhenyi"), me, new ArpHandler() {

						@Override
						public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
							com.boya.common.model._MappingKit.mapping(arp);
							arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
							arp.addSqlTemplate("boya.sql");
							return arp;
						}
					});
		}

		if (PropKit.getBoolean("EN_YOUQU", false)) {
			String host = PropKit.get("YOUQU_DB", "192.168.50.222");
			String password = PropKit.get("YOUQU_DB_PWD", "zhenyi");
			addDataSource(host, "youqu", "root", password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.youqu.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("youqu/youqu.txt");
					arp.addSqlTemplate("youqu/youquAdmin.txt");
					arp.addSqlTemplate("youqu/youquLocation.txt");
					return arp;
				}
			});
		}
		// addDataSource(TEST_HOST, "dati", "root", "zhenyi", me, new
		// ArpHandler() {
		//
		// @Override
		// public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
		// com.dati.common.model._MappingKit.mapping(arp);
		// arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		// arp.addSqlTemplate("dati.sql");
		// return arp;
		// }
		// });

		// addDataSource("test.zhen-yee.com", "beikangayi", "root", "zhenyi",
		// me, new ArpHandler() {
		//
		// @Override
		// public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
		// com.zhenyi.common.model._MappingKit.mapping(arp);
		// arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		// arp.addSqlTemplate("query.sql");
		// return arp;
		// }
		// });
		// addDataSource("dev.zhen-yee.com", "yitai", "root", "Cendb", me, new
		// ArpHandler() {
		//
		// @Override
		// public void configMapping(ActiveRecordPlugin arp) {
		// }
		// });
		if (PropKit.getBoolean("EN_JIAHUA", false)) {
			LogKit.info("en jiahua");
			String scheme = PropKit.get("JH_SCHEME", "jiahua");
			addDataSource(PropKit.get("JH_DB", "192.168.50.222"), scheme, "root", PropKit.get("JH_DB_PWD", "zhenyi"),
					me, new ArpHandler() {
						// addDataSource("localhost", "jiahua", "root", "Cendb",
						// me,
						// new
						// ArpHandler() {

						@Override
						public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
							com.jiahua.common.model._MappingKit.mapping(arp);
							arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
							arp.addSqlTemplate("jiahua.txt");
							arp.addSqlTemplate("jh_question.txt");
							arp.addSqlTemplate("jh_role.txt");
							arp.addSqlTemplate("jh_api.txt");
							return arp;
						}
					});
		}
		if (PropKit.getBoolean("EN_Zhenyi", false)) {
			String scheme = PropKit.get("ZY_SCHEME", "zhenyitech");
			System.out.println("scheme " + scheme);
			String host = PropKit.get("ZY_DB", "192.168.50.222");
			String user = PropKit.get("ZY_DB_USER", "root");
			String password = PropKit.get("ZY_DB_PWD", "zhenyi");
			System.out.println("password: " + password);
			addDataSource(host, scheme, user, password, me, new ArpHandler() {

				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.zhenyitech.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("zhenyitech_dingke.txt");
					return arp;
				}
			});
		}
		if (PropKit.getBoolean("EN_ZHAOZHENG", false)) {
			String scheme = PropKit.get("ZZ_SCHEME", "zhaozheng");
			String host = PropKit.get("ZZ_DB", "192.168.50.222");
			String user = PropKit.get("ZZ_DB_USER", "root");
			String password = PropKit.get("ZZ_DB_PWD", "zhenyi");
			addDataSource(host, scheme, user, password, me, new ArpHandler() {
				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.zhaozheng.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("zhaozheng/zhaozheng.txt");
					return arp;
				}
			});
		}
		if (PropKit.getBoolean("EN_ZHENGFANG", false)) {
			String host = PropKit.get("ZHENGFANG_DB", "192.168.50.222");
			String password = PropKit.get("ZHENGFANG_DB_PWD", "zhenyi");
			addDataSource(host, "zhengfang", "root", password, me, new ArpHandler() {
				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.zhengfang.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("zhengfang/zhengfangAdmin.txt");
					arp.addSqlTemplate("zhengfang/zhengfang.txt");
					return arp;
				}
			});
		}
		if (PropKit.getBoolean("EN_YITAI_CMS", false)) {
			String host = PropKit.get("YITAI_DB", "yitai.zhen-yee.com");
			String password = PropKit.get("YITAI_DB_PWD", "yitaikeji");
			addDataSource(host, "cms", "root", password, "yitaicms", me, new ArpHandler() {
				@Override
				public ActiveRecordPlugin configMapping(ActiveRecordPlugin arp) {
					com.yitaicms.common.model._MappingKit.mapping(arp);
					arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
					arp.addSqlTemplate("yitai/yitai.txt");
					return arp;
				}
			});
		}
	}

	protected void addDataSource(String host, String scheme, String user, String password, String name, Plugins me,
			ArpHandler handler) {
		LogKit.info("connect data:");
		LogKit.info("host: " + host);
		LogKit.info("scheme: " + scheme);
		LogKit.info("user: " + user);
		String jdbc = "jdbc:mysql://" + host + "/" + scheme + "?autoReconnect=true&useUnicode=true";
		// 配置C3p0数据库连接池插件
		C3p0Plugin druidPlugin = new C3p0Plugin(jdbc, user, password);
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(name, druidPlugin);
		arp = handler.configMapping(arp);
		me.add(arp);
	}

	// @Override
	// protected void configSQL(ActiveRecordPlugin arp) {
	// // TODO Auto-generated method stub
	// super.configSQL(arp);
	// // arp.addSqlTemplate("query.sql");
	// com.zhenyi.common.model._MappingKit.mapping(arp);
	// }

	@Override
	public void configPlugin(Plugins me) {
		super.configPlugin(me);
		boolean debug = PropKit.getBoolean("devMode", false);
		if (!debug) {
			// cp.addTask("*/5 * * * *", new JHOnCourseTask());
			if (PropKit.getBoolean("EN_JIAHUA", false)) {
				Cron4jPlugin jhCron = new Cron4jPlugin("jiahuaTask.txt", "jiahuaTask");
				me.add(jhCron);
				LogKit.info("trigger jiahua task");
			}
			if (PropKit.getBoolean("EN_YOUQU", false)) {
				LogKit.info("yq Task");
				Cron4jPlugin jhCron = new Cron4jPlugin("/youqu/youquTask.txt", "youquTask");
				me.add(jhCron);
				LogKit.info("trigger youquTask");
			}
		}
		// Cron4jPlugin cp = new Cron4jPlugin();
		// cp.addTask("*/1 * * * *", new GroupTask());
		// me.add(cp);

	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void afterJFinalStart() {
		try {
			String routePackage = "";
			Class c1 = Class.forName("com.cenjiawen.common.ReleaseConfig");
			if (c1 == null) {
				LogKit.info("no class");
			} else {
				LogKit.info("go class");
			}
			// Object o = c1.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*
		 * String appid = "wx4f4a15d5e7b7d03c"; String secret = ""; String token = "";
		 * addWX(appid, secret, "", false, token);
		 */

		String appId = PropKit.get("TP_APP_ID");
		if (StrKit.notBlank(appId)) {
			System.out.println("start appId " + appId);
			String token = PropKit.get("token");
			String appSecret = PropKit.get("appSecret");
			String encodingAesKey = PropKit.get("encodingAesKey");
			ApiConfig config = new ApiConfig(token, appId, appSecret);
			config.setEncodingAesKey(encodingAesKey);
			config.setEncryptMessage(true);
			ApiConfigKit.putApiConfig(config);
			ApiConfigKit.setThreadLocalAppId(appId);
		}

		// ApiConfig lmConfig = new ApiConfig(PropKit.get("LM_TOKEN",
		// "LM_TOKEN"), PropKit.get("LM_APP_ID"),
		// PropKit.get("LM_APP_SECRET"));
		// lmConfig.setEncodingAesKey(PropKit.get("LM_AES_KEY"));
		// lmConfig.setEncryptMessage(PropKit.getBoolean("LM_ENCRYPT", false));
		// ApiConfigKit.putApiConfig(lmConfig);

		// ApiConfig dtConfig = new ApiConfig(PropKit.get("DT_TOKEN",
		// "DT_TOKEN"), PropKit.get("DT_APP_ID"),
		// PropKit.get("DT_APP_SECRET"));
		// dtConfig.setEncodingAesKey(PropKit.get("DT_AES_KEY"));
		// dtConfig.setEncryptMessage(PropKit.getBoolean("DT_ENCRYPT", false));
		// ApiConfigKit.putApiConfig(dtConfig);

		// ApiConfig pxlmConfig = new ApiConfig();
		// pxlmConfig.setAppId(PropKit.get("PXLM_APP_ID", "LM_TOKEN"));
		// pxlmConfig.setAppSecret(PropKit.get("PXLM_APP_SECRET", "LM_TOKEN"));
		// pxlmConfig.setEncodingAesKey(PropKit.get("PXLM_AES_KEY",
		// "LM_TOKEN"));
		// pxlmConfig.setEncryptMessage(false);
		// pxlmConfig.setToken(PropKit.get("PXLM_TOKEN", "LM_TOKEN"));
		// ApiConfigKit.putApiConfig(pxlmConfig);

		if (PropKit.getBoolean("EN_LIUXUE", false)) {

			ApiConfig lxConfig = new ApiConfig();
			lxConfig.setAppId(PropKit.get("LX_APP_ID", "LX_TOKEN"));
			lxConfig.setAppSecret(PropKit.get("LX_APP_SECRET", "LM_TOKEN"));
			lxConfig.setEncodingAesKey(PropKit.get("LX_AES_KEY", "LM_TOKEN"));
			lxConfig.setEncryptMessage(false);
			lxConfig.setToken(PropKit.get("LX_TOKEN", "LM_TOKEN"));
			ApiConfigKit.putApiConfig(lxConfig);

			ApiConfig apiConfig = new ApiConfig();
			apiConfig.setAppId(PropKit.get("LX_APP_ID_GZH", ""));
			apiConfig.setAppSecret(PropKit.get("LX_APP_SECRET_GZH", ""));
			apiConfig.setEncodingAesKey(PropKit.get("LX_AES_KEY_GZH", ""));
			apiConfig.setEncryptMessage(false);
			apiConfig.setToken(PropKit.get("LX_TOKEN_GZH", ""));
			ApiConfigKit.putApiConfig(apiConfig);
		}

		// if (PropKit.getBoolean("EN_SMS", false)) {
		// new Thread(new Runnable() {
		// public void run() {
		// DXUtils dxUtils = new DXUtils();
		// try {
		// dxUtils.startWS();
		// } catch (URISyntaxException e) {
		// e.printStackTrace();
		// }
		// }
		// }).start();
		// }

		super.afterJFinalStart();

		try {
			if (PropKit.getBoolean("EN_JIAHUA", false)) {
				WSServer wsServer = Singleton.get(WSServer.class);
				wsServer.start();
			}
			if (PropKit.getBoolean("EN_WUMAIN", false)) {
				// wsUtilsPro.start();
			}
		} catch (Exception e) {
			LogKit.error("start ws fail");
		}
		// new ImportUtils().run();
	}

	private WSUtilsPro wsUtilsPro = new WSUtilsPro(18989, "/wmws", new WebSocketChannelInitializer2() {

		@Override
		public SimpleChannelInboundHandler<TextWebSocketFrame> onAddHandler() {
			return new Handler();
		}
	});

	@Override
	public void beforeJFinalStop() {

		try {
			WSServer wsServer = Singleton.get(WSServer.class);
			wsServer.stop();
			if (PropKit.getBoolean("EN_WUMAIN", false)) {
				// wsUtilsPro.stop();
			}
		} catch (Exception e) {
			LogKit.error("stop ws fail");
		}
		super.beforeJFinalStop();
	}

}
